﻿using System;
using System.Collections;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using Project5188.BUS;
using Project5188.BUS.Operations;
using ExportToExcelTools;
using Project5188.Client.ClientCodes;

namespace Project5188.Client.Operations
{
    /// <summary>
    /// Interaction logic for InstitutionalScreenPage.xaml
    /// </summary>
    public partial class InstitutionalPage
    {
        private CollectionViewSource _resources;

        public InstitutionalPage()
            : base(Extensions.PageNames.InstitutionalPage)
        {
            InitializeComponent();

            pagingControl.DataChanged += PagingControlDataChanged;
        }

        protected override void OnPreviewTextInput(TextCompositionEventArgs e)
        {
            if (((FrameworkElement)(e.OriginalSource)).Name == txtInstitutionalNo.Name)
            {
                e.Handled = !AreAllValidNumericChars(e.Text);
            }
            base.OnPreviewTextInput(e);
        }

        void PagingControlDataChanged(object sender, EventArgs e)
        {
            if (_resources != null) _resources.Source = pagingControl.Paging.GetDataList();
            if (_resources != null) dgInstitutions.ItemsSource = _resources.Source as IEnumerable;
        }

        private void BtnAddClick(object sender, RoutedEventArgs e)
        {
            var insert = new InsertWindow();
            insert.Closed += InsertClosed;
            DetailView<institution> institutionDetail = new Institutions();
            insert.SetEntity(institutionDetail, Extensions.PageNames.InstitutionalPage.ToString());
            insert.Show();
        }

        public override bool Search()
        {
            if (base.Search())
            {
                var institutions = new Institutions();
                _resources = Resources["institutionalViewSource"] as CollectionViewSource;
                pagingControl.SetDataSource(
                    institutions.GetEntityListByParameters(txtInstitutionalNo.Text, txtInstutionalName.Text,
                                                           dpAggrementStartDate.SelectedDate,
                                                           dpAggrementEndDate.SelectedDate), 1, 10);
                if (_resources != null) _resources.Source = pagingControl.Paging.GetDataList();
                if (_resources != null) dgInstitutions.ItemsSource = _resources.Source as IEnumerable;
            }
            else if (TransactionInfo.TransactionId.HasValue)
            {
                var institutions = new Institutions();
                _resources = Resources["institutionalViewSource"] as CollectionViewSource;
                pagingControl.SetDataSource(
                    institutions.GetEntityById(TransactionInfo.TransactionId.Value), 1, 10);
                if (_resources != null) _resources.Source = pagingControl.Paging.GetDataList();
                if (_resources != null) dgInstitutions.ItemsSource = _resources.Source as IEnumerable;
            }

            return true;
        }

        private void BtnDetailClick(object sender, RoutedEventArgs e)
        {
            var institutionValue = (institution)dgInstitutions.SelectedItem;
            var detail = new DetailWindow();
            DetailView<institution> institutionValueDetail = new Institutions { Entity = institutionValue };
            detail.SetEntity(institutionValueDetail, Extensions.PageNames.InstitutionalPage.ToString());
            detail.Show();
        }

        private void BtnUpdateClick(object sender, RoutedEventArgs e)
        {
            var institutionValue = (institution)dgInstitutions.SelectedItem;
            var update = new UpdateWindow();
            update.Closed += UpdateClosed;
            DetailView<institution> institutionValueDetail = new Institutions();
            update.SetEntity(institutionValueDetail, Extensions.PageNames.InstitutionalPage.ToString(), institutionValue.a_ID);
            update.Show();
        }

        private void BtnDeleteClick(object sender, RoutedEventArgs e)
        {
            var institutionValue = (institution)dgInstitutions.SelectedItem;
            DetailView<institution> institutionValueDetail = new Institutions();
            GenerateDeleteDialog(institutionValueDetail, institutionValue.a_ID);
        }

        private void BtnPrintClick(object sender, RoutedEventArgs e)
        {
            try
            {
                dgInstitutions.ExportToExcel();
            }
            catch
            {
                MessageBox.Show("Excel'e aktarma sırasında hata oluştu.");
            }
        }
    }
}
